<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of indexAction
 *
 * @author xlp
 */
class indexAction extends commonAction {

    function __construct() {
        parent::__construct();
        //获取游戏数据
        parent::_initHeader();
        $this->assign('trades' , array('ji' => '寄', 'ling' => '零', 'bao' => '保', 'shang' => '商'));
        $this->assign('colors' , array('uu898'=>'uu' , 'dd373'=>'dd','17uoo'=>'uoo','ee979'=>'ee'));
    }

    /*
     * 首页
     */
    function index() {
      $this->display();
    }



     /*
     * 游戏列表
     */
    function game() {
      $this->display();
    }



    /*
     * 选择区服信息
    */
    function area() {
      $gid = $this->_getid('gid' , 0);
      if(!$gid) {
        showError('参数丢失');
      }
      $where['game_id'] = $gid;
      $areas   = D('game_area')->where($where)->order("`sort`")->findAll();
      $where['is_show'] = 1;
      $stdmodes = D('stdmode')->where($where)->order("`sort`")->findAll();
      $abridge  = D('game')->field("abridge")->where(array('id'=>$gid))->find();
      $this->assign('stdmodes' , $stdmodes);
      $this->assign('areas' , $areas);
      $this->assign('gm' , $abridge['abridge']);
      $this->display();
    }




    /*
     * 比价检索
     */
    function search() {
      $gm = $this->_post('gm');
      $ar = $this->_post('ar');
      $ty = $this->_post('ty');
      $cr = $this->_post('cr');
      if(!$gm || !$ar || !$ty || !$cr) {
        jumpTo(U('index'));
      }
      $serverInfo = D('game_server')->field("`server_name`")->where(array('abridge'=>$ar))->find();
      $typeInfo = D('stdmode')->field("`game_id`,`name`")->where(array('abridge'=>$ty))->find();
      $types = D('stdmode')->where(array('game_id'=>$typeInfo['game_id'],'is_show'=>1))->order("`sort`")->findAll();
      //获取平台信息
      $platforms   = D('platform')->where(array('status'=>1))->order('sort')->findAll();
      $platCookies = myCookie('platforms') ? explode('-', myCookie('platforms')) : array();
      //默认检索所有平台
      if(!$platCookies) {
        $this->setPlatCookieAll($platforms);
      }
      foreach ($platforms as $k => &$v) {
        if($platCookies){
          if(in_array($v['abridge'], $platCookies)) {
            $v['is_check'] = true;
          }else{
            $v['is_check'] = false;
          }
        }else{
          $v['is_check'] = true;
        }
      }
      $this->assign('platforms' , $platforms);
      $this->assign(array('gm'=>$gm , 'ar'=>$ar, 'ty'=>$ty,'cr'=>$cr));
      $this->assign(array('server'=>$serverInfo['server_name'] , 'type'=>$typeInfo['name'],'types'=>$types));
    	$this->display();
    }


    /*
     * 行情趋势
     */
    function records() {
      $gm = $this->_post('gm');
      $ar = $this->_post('ar');
      $ty = $this->_post('ty');
      $cr = $this->_post('cr');
      if(!$gm || !$ar || !$ty || !$cr) {
        jumpTo(U('index'));
      }
      $serverInfo = D('game_server')->field("`server_name`")->where(array('abridge'=>$ar))->find();
      $typeInfo = D('stdmode')->field("`game_id`,`name`")->where(array('abridge'=>$ty))->find();
      $types = D('stdmode')->where(array('game_id'=>$typeInfo['game_id'],'is_show'=>1))->order("`sort`")->findAll();

      $this->assign(array('gm'=>$gm , 'ar'=>$ar, 'ty'=>$ty,'cr'=>$cr));
      $this->assign(array('server'=>$serverInfo['server_name'] , 'type'=>$typeInfo['name'],'types'=>$types));
      $this->display();
    }



    /*
     * 获取实时数据
     */
    function getRealData() {
      $gm = $this->_post('g');
      $ar = $this->_post('a');
      $ty = $this->_post('t');
      $cr = $this->_post('c');
      $unit = M('stdmode')->field("`unit`")->where(array('abridge'=>$ty))->find();
      $resultData = array();
      //判断ip受限次数
      $isLimit = $this->_checkUserIP();
      if(isHave($isLimit)) {
          $this->JsonReturn('error', $isLimit);
      }else{
          T('user/parity');
          $sendPost['game'] = $gm;
          $sendPost['areaName'] = $ar;
          $tempPlat = explode('-', ltrim(myCookie('platforms'),'-'));
          $sendPost['platforms'] = $tempPlat;
          $sendPost['trader'] = $cr;
          $resultData = parity::sendData($sendPost);
          // z($resultData);
          $this->assign('list' , $resultData);
          $this->assign('unit' , $unit['unit']);
          if($cr == 'buyer') {
            $this->JsonReturn('ok', $this->getFetch('buyer_item'), 1);
          }else{
            $this->JsonReturn('ok', $this->getFetch('seller_item'), 1);
          }
      } 
    }


    /*
     * 获取历史详情
     */
    function getRecordsData() {
      $gm = $this->_post('g');
      $ar = $this->_post('a');
      $ty = $this->_post('t');
      $cr = $this->_post('c');
      $year  = $this->_post('year' , date('Y'));
      $month = $this->_post('month', date('m'));
      $day   = $this->_post('day' , date('d'));
      $density = $this->_post('density','day');
      $etime = $year.'/'.$month.'/'.$day;
      $resultData = array();
      T('user/parity');
      $sendData['trader'] = $cr;
      $sendData['areas'][]  = $ar;
      $sendData['timeDensity'] = $density;
      switch ($density) {
        case 'day':
          $sendData['start'] = date('Y/m/d', strtotime("$etime -11day"));
          $sendData['end'] = $etime;
          break;
        case 'month':
          $sendData['start'] = date('Y/m' , strtotime("$etime -11month"));
          $sendData['end'] = date('Y/m' , strtotime($etime));
          break;
        case 'year':
          $start = $etime - 10;
          $end = $etime + 1;
          $sendData['start'] = (string) $start;
          $sendData['end'] = (string) $end;
          break;  
      }
      // var_dump($sendData);
      // exit;
      $resultData = parity::sendRecordsData($sendData);
      $this->assign('list' , $resultData);
      $this->assign('density' , $density);
      $this->JsonReturn('ok', $this->getFetch('records_item') , 1);
    }




    /*
     * 获取ip访问次数
     */
    function _checkUserIP() {
        $ip = getUserIp();
        $where['ip'] = $ip;
        $result = M('ip_log')->where($where)->find();
        if(!$result) {
            $objData['ip'] = $ip;
            $objData['count'] = 1;
            $objData['stime'] = TIME;
            $objData['etime'] = TIME + 60;
            M('ip_log')->insert($objData);
            return false;
        }else{
            M('ip_log')->where(array('id' => $result['id']))->setInc('count' , 1);
            $count = $result['count'] + 1;
            if(TIME > $result['etime']) {
              M('ip_log')->where(array('id' => $result['id']))->delete();
              return false;
            }else{
                if(user::$userInfo['uid']) {
                    $level = user::$userInfo['user_level'];
                }else{
                    $level = 1;
                }  
                $data = array();
                switch ($level) {
                  case '1':
                    if($count > 15) {
                      $data['count'] = $count;
                      $data['limit'] = 15;
                    }
                    break;
                  case '2':
                    if($count > 20) {
                      $data['count'] = $count;
                      $data['limit'] = 20;
                    }
                    break;
                  case '3':
                    if($count > 30) {
                      $data['count'] = $count;
                      $data['limit'] = 30;
                    }
                    break;  
                }
                return $data;
            }
        }
    }




    /*
     * 根据 area_id 获取游戏服
     */
   	function getServerByAreaId() {
   		$area_id = $this->_getid('area_id' , 0);
   		if(!$area_id) {
   			$this->JsonReturn('参数丢失');
   		}
   		$where['area_id'] = $area_id;
   		$servers = D('game_server')->where($where)->order("`sort`")->findAll();
   		$this->assign('servers' , $servers);
      $this->JsonReturn('ok', $this->getFetch('server_item'), 1);
   	}



     /*
     * 根据 area_id 获取游戏服
     */
    function getServer() {
      $area_id = $this->_getid('area_id' , 0);
      if(!$area_id) {
        $this->JsonReturn('参数丢失');
      }
      $where['area_id'] = $area_id;
      $servers = D('game_server')->where($where)->order("`sort`")->findAll();
      $this->assign('servers' , $servers);
      $this->JsonReturn('ok', $this->getFetch('rserver_item'), 1);
    }



   	/*
     * 根据 game_id 获取游戏大区
     */
    function getAreaByGameAbri() {
      $gm = $this->_get('gm' , 0);
      if(!$gm) {
        $this->JsonReturn('参数丢失');
      }
      $game = D('game')->field('id')->where(array('abridge'=>$gm))->find();
      $where['game_id'] = $game['id'];
      $areas = D('game_area')->where($where)->order("`sort`")->findAll();
      $this->assign('areas' , $areas);
      $this->JsonReturn('ok', $this->getFetch('area_item'), 1);
    } 


    function getArea() {
      $gm = $this->_get('gm' , 0);
      if(!$gm) {
        $this->JsonReturn('参数丢失');
      }
      $game = D('game')->field('id')->where(array('abridge'=>$gm))->find();
      $where['game_id'] = $game['id'];
      $areas = D('game_area')->where($where)->order("`sort`")->findAll();
      $this->assign('areas' , $areas);
      $this->JsonReturn('ok', $this->getFetch('rarea_item'), 1);
    }

    /*
     * 设置所有平台Cookie
     */
    protected function setPlatCookieAll($objData) {
    	if(!$objData){
    		return;
    	}
    	$platforms = array();
    	foreach ($objData as $k => $v) {
    		$platforms[] = $v['abridge'];
    	}
    	myCookie('platforms', implode('-', $platforms));
    }


    /*
     * 设置平台筛选Cookie
     */
    function setPlatCookie() {
    	$is_check = $this->_getid('is_check');
    	$abridge  = $this->_get('abridge');
    	$platCookies = explode('-', ltrim(myCookie('platforms') , '-'));
    	if($is_check) {
    		if(in_array($abridge, $platCookies)) {
    			return;
    		}else{
    			array_push($platCookies, $abridge);
    			myCookie('platforms' , implode('-', $platCookies));
    		}
    	}else{
    		$key = array_search($abridge , $platCookies);
  			unset($platCookies[$key]);	
  			if(!empty($platCookies)){
  				myCookie('platforms' , implode('-', $platCookies));
  			}else{
  				myCookie('platforms' , null);
  			}
  		}
    }

}
